<?php

namespace backend\controllers;

use backend\models\Article;
use backend\models\Article_detail;
use backend\models\ArticleCategory;
use backend\models\EveryIsOk;
use yii\data\Pagination;
use yii\helpers\ArrayHelper;
use yii\web\Request;

class ArticleController extends \yii\web\Controller
{

    /**
     * 添加行为控制过滤
     * @return array
     */
    public function behaviors()
    {
        return [
            'access'=>[
                'class'=>'backend\filter\AccessFilter',
            ],
        ];
    }

    /**
     * @return string
     */
    public function actionIndex()
    {
        return $this->render('index');
    }

    /*
     * 实现添加文章的功能
     */
    public function actionAdd(){
        //实例化表单模型
        $model = new Article();
        //实例化文章细节的对象
        $model->contentObj  = new Article_detail();
        //实例化请求对象
        $request = new Request();
        //判断请求方式并且加载数据
        if($model->load($request->post()) && $model->contentObj ->load($request->post())){
            //保存数据
            $rs = $model->saveData();
            //提示
            EveryIsOk::setTisiInfo($rs,'编辑文章');
            //跳转到文章列表页
            return $this->redirect(['article/list']);
        };

        //显示添加文章的表单视图
        return $this->render('add',['model'=>$model,'articleDetail'=>$model->contentObj ]);
    }
    
    
    /*
     * 实现文章的列表功能
     */
    public function actionList()
    {
        //实例化表单模型
        $query = Article::find()->where(['!=','status',-1]);
        //调用方法得到分页的对象
        $pager = EveryIsOk::getPager($query,10);
        //获取当前页码的数据
        $models = $query->limit($pager->limit)->offset($pager->offset)->all();
        //显示视图
        return $this->render('list',['models'=>$models,'pager'=>$pager]);
    }

    /*
     * 实现文章的修改
     */
    public function actionEdit($id)
    {
        //实例化表单模型
        $model =Article::findOne(['id'=>$id]);
        //实例化文章细节的对象
        $model->contentObj  = Article_detail::findOne(['article_id'=>$model->id]);
        //实例化请求对象
        $request = new Request();
        //判断请求方式并且加载数据
        if($model->load($request->post()) && $model->contentObj->load($request->post())){
            $rs = $model->saveData();
            EveryIsOk::setTisiInfo($rs,'编辑文章');
            //跳转到文章列表页
            return $this->redirect(['article/list']);
        };

        //显示添加文章的表单视图
        return $this->render('add',['model'=>$model,'articleDetail'=>$model->contentObj]);
    }

    /*
     * 实现删除的功能
     */
    public function actionDel($id)
    {
        //把对应的数据中的状态值更新为-1
        $rs = \Yii::$app->db->createCommand()->update('article',['status'=>-1],['id'=>$id])->execute();
        //执行成功后提示用户并且,跳转到列表页
        EveryIsOk::setTisiInfo($rs,'删除');
        return $this->redirect(['article/list']);
    }

    /*
 * 显示分类的回收站
 */
    public function actionRecycleBin(){
        //实例化表单模型
        $query = Article::find()->where(['=','status',-1]);
        //调用方法得到分页的对象
        $pager = EveryIsOk::getPager($query);
        //获取当前页码的数据
        $models = $query->limit($pager->limit)->offset($pager->offset)->all();

        //显示视图
        return $this->render('recycleBin',['models'=>$models,'pager'=>$pager]);
    }

    /*
     * 恢复删除的文章
     */
    public function actionRecovery($id)
    {
        //把对应的数据中的状态值更新为1
        $rs = \Yii::$app->db->createCommand()->update('article',['status'=>1],['id'=>$id])->execute();
        //执行成功后提示用户并且,跳转到列表页
        \backend\models\EveryIsOk::setTisiInfo($rs,'恢复');

        return $this->redirect(['article/list']);
    }
    /*
     * 实现文章的彻底删除
     */
    public function actionDelete($id){
        $rs = \Yii::$app->db->createCommand()->delete('article',['id'=>$id])->execute();

        \backend\models\EveryIsOk::setTisiInfo($rs,'彻底删除');

        return $this->redirect(['article/recycle-bin']);

    }

    /*
     * 实现显示的功能
     */
    public function actionShow($id)
    {
        //实例化表单模型
        //实例化表单模型
        $model =Article::findOne(['id'=>$id]);
        //实例化文章细节的对象
        $content = Article_detail::findOne(['article_id'=>$model->id]);

        //显示添加品牌的视图
        return $this->render('show',['model'=>$model,'content'=>$content]);
    }

}
